import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:kq_flutter_tv_widgets/widgets/chart/kq_chart_model.dart';

import '../../resources/kq_theme_colors.dart';

class ChartLineModel extends GetxController {
  ///通用图标布局
  KqChartModel chartModel;

  ///输入数据集合
  List<List<ChartLineValue>> chartArry;

  ChartLineModel({
    required this.chartModel,
    this.chartArry = const [],
  });
  /**以下均为内部计算无需传入 */
  ///实际高度
  double yHeight = 0;

  ///实际宽度
  double xWidth = 0;

  ///横坐标数组
  List<double> xList = [];

  ///横坐标内容
  List xValue = [];

  ///纵坐标数组
  List yList = [];

  ///纵坐标内容
  List yValue = [];

  ///动画是否结束
  bool isAnimationEnd = false;

  ///横坐标间距
  double crossSpacing = 0;

  void setChartData() {
    getAbscissaList();
    getOrdinateList();
  }

  void _initData() {
    chartModel.xFont = chartModel.xFont == 0 ? 14.sp : chartModel.xFont;
    chartModel.yFont = chartModel.yFont == 0 ? 14.sp : chartModel.yFont;
    chartModel.yTop = chartModel.yTop == 0 ? 20.r : chartModel.yTop;
    chartModel.yBottom = chartModel.yBottom == 0 ? 30.r : chartModel.yBottom;
    chartModel.xLeft = chartModel.xLeft == 0 ? 40.r : chartModel.xLeft;
    chartModel.xRight = chartModel.xRight == 0 ? 20.r : chartModel.xRight;
    yHeight = chartModel.sizeHeight - chartModel.yTop - chartModel.yBottom;
    xWidth = chartModel.sizeWidth - chartModel.xLeft - chartModel.xRight;
    chartModel.pointRadius =
        chartModel.pointRadius == 0 ? 2.r : chartModel.pointRadius;
  }

  ///设置内容
  void setValueList(List<List<ChartLineValue>> list) {
    chartModel.crossCount =
        chartModel.crossCount == 0 ? list.first.length : chartModel.crossCount;
    _initData();
    chartArry = list;
    if (!chartModel.isFixMaxY) {
      chartArry.map((List<ChartLineValue> e) {
        e.map((ChartLineValue value) {
          double y = double.parse(value.yValue);
          chartModel.maxY = chartModel.maxY > y ? chartModel.maxY : y;
        }).toList();
      }).toList();
    }
    setChartData();
    chartArry.map((e) {
      setXpointToValueList(e);
      setYpointToValueList(e);
    }).toList();
  }

  ///获取横坐标数组
  void getAbscissaList() {
    crossSpacing = xWidth / (chartModel.crossCount - 1);
    xList = [];
    for (int i = 0; i < chartModel.crossCount; i++) {
      double absPoint = crossSpacing * i + chartModel.xLeft;
      xList.add(absPoint);
    }
  }

  ///获取纵坐标数组
  void getOrdinateList() {
    double rowSpacing = yHeight / (chartModel.rowCount - 1);
    double ySpac = chartModel.maxY / (chartModel.rowCount - 1);
    yList = [];
    yValue = [];
    for (int i = 0; i < chartModel.rowCount; i++) {
      yValue.add((ySpac * (chartModel.rowCount - i - 1)).toString());
      yList.add(rowSpacing * i + chartModel.yTop);
    }
  }

  ///给内容数组赋值横坐标值
  void setXpointToValueList(List<ChartLineValue> valueList) {
    xValue = [];
    for (int i = 0; i < valueList.length; i++) {
      ChartLineValue value = valueList[i];
      value.xPoint = xList[i];
      xValue.add(value.xValue);
    }
  }

  ///给内容数组赋值纵坐标值
  void setYpointToValueList(List<ChartLineValue> valueList) {
    for (int i = 0; i < valueList.length; i++) {
      ChartLineValue value = valueList[i];
      double yHvalue = double.parse(value.yValue);
      value.yPoint = ((chartModel.maxY - yHvalue) / chartModel.maxY) * yHeight +
          chartModel.yTop;
    }
  }
}

class ChartLineValue {
  String xValue;
  String yValue;

  ///文案样式
  TextStyle style;

  ///当点击或者滑动到该区域时是否显示文案，只针对isShowValue=fase且isShowClickValue或isShowSlideValue为True才会被使用
  bool isShow;

  ///坐标线颜色
  Color lineColor;

  ///是否显示内容点的分割线
  bool isCountDivider;

  ///内容分割线的颜色
  Color dividerColor;

  ///分割线是否为实现，默认为false
  bool dividerLineSolid;

  ///遮罩层颜色
  List<Color> shaderColors;

  ///以下为自动计算，无需传入
  double xPoint;
  double yPoint;
  ChartLineValue(
      {required this.xValue,
      required this.yValue,
      this.lineColor = KqThemeColors.bgBlue,
      this.style = const TextStyle(fontSize: 12, color: KqThemeColors.text666),
      this.isCountDivider = false,
      this.dividerLineSolid = false,
      this.dividerColor = KqThemeColors.bgBlue,
      this.isShow = false,
      this.shaderColors = const [
        KqThemeColors.bgBlue,
        KqThemeColors.bgWhite,
      ],
      this.xPoint = 0,
      this.yPoint = 0});
}
